package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.GoodsType;
import com.xq.shop.dao.mapper.GoodsTypeMapper;
import com.xq.shop.dto.EditGoodsTypeDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.service.IFileService;
import com.xq.shop.service.IGoodsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.utils.MyConstants;
import com.xq.shop.vo.GoodsTypeVo;
import enums.DataStatus;
import exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-18
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {


    private final IFileService fileService;

    @Resource
    private Redis redis;

    @Autowired
    public GoodsTypeServiceImpl(IFileService fileService) {
        this.fileService = fileService;
    }

    @Override
    public List<GoodsTypeVo> getGoodsTypeTree() {
        LambdaQueryWrapper<GoodsType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsType::getStatus, DataStatus.NORMAL.getCode());
        List<GoodsType> goodsTypeList = list(wrapper);
        List<Integer> idList = new ArrayList<>();
        goodsTypeList.forEach(g -> {
            if (Objects.nonNull(g.getImageId())) {
                idList.add(g.getImageId());
            }
        });
        Map<Integer, String> map = fileService.selectFileUrl(idList);
        goodsTypeList.forEach(g -> {
            if (Objects.nonNull(g.getImageId())) {
                g.setImageUrl(map.get(g.getImageId()));
            }
        });
        List<GoodsTypeVo> voList = beanToVo(goodsTypeList);
        return MyConstants.buildGoodsTypeTree(voList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editGoodsType(EditGoodsTypeDto dto, Integer id) {
        if (dto.getMark() != 1) {
            LambdaQueryWrapper<GoodsType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GoodsType::getStatus, DataStatus.NORMAL.getCode());
            wrapper.eq(GoodsType::getMark, 1);
            wrapper.eq(GoodsType::getSearchName, dto.getSearchName());
            if (dto.getId() != 0) {
                wrapper.ne(GoodsType::getId, dto.getId());
            }
            GoodsType gt = getOne(wrapper);
            if (Objects.nonNull(gt)) {
                throw new BusinessException("DATA REPEAT", "搜索词重复");
            }
        }
        GoodsType goodsType = new GoodsType();
        if (dto.getId().equals(0)) {
            goodsType.setCreateUser(id);
            BeanUtils.copyProperties(dto, goodsType);
            save(goodsType);
        } else {
            goodsType = getById(dto.getId());
            goodsType.setUpdateUser(id);
            BeanUtils.copyProperties(dto, goodsType);
            updateById(goodsType);
        }
        redis.remove("category");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeGoodsType(IntegerDto dto, Integer id) {
        GoodsType goodsType = getById(dto.getValue());
        goodsType.setStatus(DataStatus.INVALID.getCode());
        goodsType.setUpdateUser(id);
//        LambdaQueryWrapper<GoodsType> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(GoodsType::getStatus, DataStatus.NORMAL.getCode());
//        wrapper.eq(GoodsType::getFatherId, dto.getValue());
//        List<GoodsType> goodsTypeList = list(wrapper);
//        if (!goodsTypeList.isEmpty()) {
//            goodsTypeList.forEach(gt -> {
//                gt.setStatus(DataStatus.INVALID.getCode());
//                gt.setUpdateUser(id);
//            });
//            updateBatchById(goodsTypeList);
//        }
        updateById(goodsType);
        redis.remove("category");
    }

    private List<GoodsTypeVo> beanToVo(List<GoodsType> goodsTypeList) {
        List<GoodsTypeVo> voList = new ArrayList<>();
        goodsTypeList.forEach(m -> {
            GoodsTypeVo vo = new GoodsTypeVo();
            BeanUtils.copyProperties(m, vo);
            voList.add(vo);
        });
        return voList;
    }
}
